#include "DataTrans.h"
#include "PackDefine.h"
#include <QTime>

uint32_t DataTrans::ElectricUnit(double var)
{
	return uint32_t(var * 10000.0);
}

double DataTrans::fromElectricUnit(int var)
{
	return var / 10000.0;
}

uint32_t DataTrans::toTempUnit(double var)
{
	return uint32_t(var * 10);
}
double DataTrans::fromTempUnit(int var)
{
	return var / 10;
}

uint32_t DataTrans::toVoltageUnit(double var)
{
	return uint32_t(var * 10000);
}

double DataTrans::fromVoltageUnit(int var)
{
	return var / 10000.0;
}

QString DataTrans::fromTimeUnit(uint32_t var)
{
	int day = var / MS_PER_DAY;
	QTime time(0,0);
	time = time.addMSecs(var % MS_PER_DAY);

	int hh = day * 24 + time.hour();
	return QString::asprintf("%02d",hh) + ":" + time.toString("mm:ss:zzz");
}

QString DataTrans::fromTimeUnitSec(uint32_t var)
{
	int day = var / MS_PER_DAY;
	QTime time(0, 0);
	time = time.addMSecs(var % MS_PER_DAY);

	int hh = day * 24 + time.hour();
	return QString::asprintf("%02d", hh) + ":" + time.toString("mm:ss");
}

double DataTrans::fromEngUnit(int64_t var)
{
	return var / 3600.0 / 10000.0;
}

double DataTrans::fromCapUnit(int64_t var)
{
	return var / 3600.0 / 10000.0;
}

uint32_t DataTrans::toTimeUnit(QString text)
{
	QStringList list = text.split(":");
	Q_ASSERT(list.size() == 4);

	QTime time;
	time = time.fromString(text.mid(3), "mm:ss:zzz");

	int hh = list[0].toInt();
	return hh * (3600 * 1000) + time.msecsSinceStartOfDay();
}